﻿// JScript File

  // инициализация ;) 
  
var itemFasade;
var timerFasade;
var autoSaveFasade;  
var timerPointer;
var appPointer;
var autoSaveTimerPointer;
var titleFasade;  
var waitAttemptCount;  // количество повторов запроса к серверу
var networkErrorFasade;

function Init()
{
    window.status = "загрузка...";
                
    itemFasade = document.getElementById("itemFasade");            
    timerFasade = document.getElementById("timerFasade");    
    titleFasade = document.getElementById("titleFasade");           
    networkErrorFasade = document.getElementById("lblNetworkError");   
    waitAttemptCount = 0;
    
    RequestServer();
}

function RequestServer()
{   
    var xml = CreateXmlDOM();
    var sm = new ServerManager();

    try
    {
        if (sm.GetAssessment(xml, assignmentId) != SM_OK)
            ShowError(ERR_INIT + "<br/>" + sm.errorText);
    }
    catch (e)
    {
        ShowError(ERR_INIT + "<br/>" + sm.errorText + ": " + (e.message == undefined ? e : e.message));
    }  
}

function ProcessStart(xml) // after server has resposned
{   
    
    
    if (errorIsShown)  // если уже произошла ошибка, то выходим
        return;
        
    HideWaitPanel();           
    
    var sm = new ServerManager();
    
    try
    {    
        var adsPlayer = new AdsPlayer(xml, itemFasade);             
    }
    catch (e)
    {
        ShowError(ERR_INIT + "<br/>" + sm.errorText + ": " + (e.message == undefined ? e : e.message));
    }
    
    var timer = new Timer(adsPlayer, timerFasade);
    
    var autoSaveTimer = new AutoSaveTimer(adsPlayer);
    

    // установление указателей
    timerPointer = timer;
    appPointer = adsPlayer;
    autoSaveTimerPointer = autoSaveTimer;   
    
    appPointer.OnLoaded= function() { try { OnTestLoaded(); } catch(e) { } }
               
    appPointer.OnPassed= function() { try { OnTestPassed(); } catch(e) { } }        
        
    try
    {        
        adsPlayer.Init();              
        timer.Init();        
        timer.Start();        
        autoSaveTimer.Start();                
    }
    catch (e)
    {        
        ShowError(ERR_UNKNOWN + ": " + (e.message == undefined ? e : e.message));
    }
    
     if (adsPlayer.items.length == 0)
        ShowError(ERR_TESTISEMPTY);
    
    window.status = "";    
}    

